package com.service.impl;

import com.dao.ResourceDao;
import com.dao.UserGpDao;
import com.entity.GoldPoints;
import com.entity.Resource;
import com.entity.User;
import com.service.ResourceService;
import com.util.AjaxResult;
import com.vo.GoldPointsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * date:2021/4/29
 * description:
 * Author:Stargazer
 */
@Service
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    private ResourceDao resourceDao;
    @Autowired
    private UserGpDao userGpDao;


    @Override
    public AjaxResult checkResourceByIdAndUser(Integer resourceId, User user) {
        //通过资源id获取从表中查出对应的资源对象
        Resource resource = resourceDao.selectResourceByID(resourceId);
        Integer costType = resource.getCost_type();//资源的支付方式0是积分,1是金币
        Integer costNumber = resource.getCost_number();//该资源需要的金币或积分
        Integer authorId = resource.getUser_id();//资源中的上传用户的id即作者id
        //用户的id
        Integer userId = user.getId();
        //根据用户id查询对相应的金币积分对象
        GoldPointsVo goldPointsVoByUserId = userGpDao.getGoldPointsVoByUserId(userId);
        Integer userSumGoldCount = goldPointsVoByUserId.getSum_gold_count();//获取对应的用户金币总量
        Integer userSumPointCount = goldPointsVoByUserId.getSum_point_count();//获取对应的用户积分总量

        //根据用户id查询对相应的金币积分对象
        GoldPointsVo goldPointsVoByAuthorId = userGpDao.getGoldPointsVoByUserId(authorId);
        Integer authorSumGoldCount = goldPointsVoByAuthorId.getSum_gold_count();//获取对应的作者金币总量
        Integer authorSumPointCount = goldPointsVoByAuthorId.getSum_point_count();//获取对应的作者积分总量
        /**
         * 先判断支付方式costType,再判断该资源需要的积分costNumber是否小于用户的积分或者金币总量
         * 由于前段需要获取data.obj.cost_type,和data.obj.file_type所以我觉得AjaxResult里面应该装对应的资源对象,之后再修改数据库的数据
         */
        AjaxResult ajaxResult = null;
        if(costType==0){//积分,
            if(costNumber<=userSumPointCount){
                //用户的goldPoints,用来修改用户积分的
                GoldPoints userPoints = new GoldPoints();
                userPoints.setUser_id(user.getId());
                userPoints.setPoint_count(-costNumber);
                userPoints.setInfo("视频花费了"+costNumber+"点积分");
                userPoints.setCreate_date(new Date());
                //用户的AuthorPoints,用来修改用户积分的
                GoldPoints authorPoints = new GoldPoints();
                authorPoints.setUser_id(resource.getUser_id());
                authorPoints.setPoint_count(costNumber);
                authorPoints.setInfo("点击视频增加了"+costNumber+"点积分");
                authorPoints.setCreate_date(new Date());
                //修改积分表
                userGpDao.addPointOrGold(userPoints);//给用户减少积分
                userGpDao.addPointOrGold(authorPoints);//给作者增加积分
                ajaxResult=new AjaxResult(true,"扣除了"+costNumber+"点积分",resource);
            }else {
                System.out.println("积分不足");
                ajaxResult.setSuccess(false);
                ajaxResult.setMsg("积分不足,积极参与活动获取更多积分吧！");
            }
        }else if(costType==1){//用金币的情况
            if(costNumber<=userSumGoldCount){
                //用户的goldPoints,用来修改用户金币
                GoldPoints uGold = new GoldPoints();
                uGold.setUser_id(user.getId());
                uGold.setGold_count(-costNumber);
                uGold.setInfo("视频花费了"+costNumber+"点金币");
                uGold.setCreate_date(new Date());
                //用户的AuthorPoints,用来修改用户积分的
                GoldPoints aGold = new GoldPoints();
                aGold.setUser_id(resource.getUser_id());
                aGold.setGold_count(costNumber);
                aGold.setInfo("点击视频增加了"+costNumber+"点金币");
                aGold.setCreate_date(new Date());
                //修改积分表
                userGpDao.addPointOrGold(uGold);
                userGpDao.addPointOrGold(aGold);
                ajaxResult=new AjaxResult(true,"扣除了"+costNumber+"点金币",resource);
            }else {
                System.out.println("金币不足");
                ajaxResult.setSuccess(false);
                ajaxResult.setMsg("金币不足,积极参与活动获取更多金币吧！");
            }
        }

        return ajaxResult;
    }
}
